package com.basic.mq.handler;

import com.basic.contant.RabbitContant;
import com.basic.dto.DynamicDTO;
import com.basic.dto.ScoreRecordDTO;
import com.basic.enums.ScoreRecordDealTypeEnum;
import com.basic.mapper.DynamicMapper;
import com.basic.mapper.ScoreRecordMapper;
import com.basic.mq.MqMessageDTO;
import com.basic.mq.MqMessageHandler;
import com.basic.mq.MqMessageTypeEnum;
import com.basic.mq.dto.CommentMessageDTO;
import com.basic.mq.dto.PublishEssayMessageDTO;
import com.basic.mq.dto.RankingListMessageDTO;
import com.basic.util.CommonUtil;
import com.basic.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;

@Component
public class CommentMessageHandler implements MqMessageHandler {

    final static Logger logger = LoggerFactory.getLogger(CommentMessageHandler.class);

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private ScoreRecordMapper scoreRecordMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void deal(MqMessageDTO messageDTO) {
        logger.info("评论产生的消息:{}", JsonUtils.objectToJson(messageDTO));

        String message = messageDTO.getMessage();

        if(StringUtils.isEmpty(message)) {
            logger.info("评论产生的消息, 消息为空");
            return;
        }

        CommentMessageDTO commentMessageDTO;

        try {
            commentMessageDTO = JsonUtils.jsonToPojo(message, CommentMessageDTO.class);
            if(commentMessageDTO == null) {
                logger.info("评论产生的消息, 消息转换为空, message:{}", message);
                return;
            }
        } catch (Exception e) {
            logger.info("评论产生的消息, 消息转换异常, message:{}, e:{}", message, JsonUtils.objectToJson(e));
            return;
        }

        DynamicDTO dynamicDTO;
        try {
            dynamicDTO = dynamicMapper.queryDynamicById(commentMessageDTO.getEssayId());
            if(dynamicDTO == null) {
                logger.info("评论产生的消息, 文章不存在, message:{}", message);
                return;
            }
        } catch (Exception e) {
            logger.info("评论产生的消息, 查询文章异常, message:{}, e:{}", message, JsonUtils.objectToJson(e));
            return;
        }

        ScoreRecordDTO scoreRecordDTO = new ScoreRecordDTO();
        scoreRecordDTO.setId(CommonUtil.createUUID());
        scoreRecordDTO.setScore(5);
        scoreRecordDTO.setSendUserId(commentMessageDTO.getSendUserId());
        scoreRecordDTO.setTakedUserId(dynamicDTO.getUserId());
        scoreRecordDTO.setDealType(ScoreRecordDealTypeEnum.COMMENT.getType());
        scoreRecordDTO.setEntityId(commentMessageDTO.getEssayId());
        scoreRecordDTO.setEntityType(0);

        scoreRecordMapper.addScoreRecordDTO(scoreRecordDTO);

        MqMessageDTO mqMessageDTO = new MqMessageDTO();
        mqMessageDTO.setId(CommonUtil.createUUID());
        mqMessageDTO.setCreateDate(new Date());
        mqMessageDTO.setType(MqMessageTypeEnum.ASYNC_DEAL_RANKING_LIST.getCode());

        RankingListMessageDTO rankingListMessageDTO = new RankingListMessageDTO();
        rankingListMessageDTO.setEssayId(scoreRecordDTO.getEntityId());
        rankingListMessageDTO.setScore(scoreRecordDTO.getScore());

        mqMessageDTO.setMessage(JsonUtils.objectToJson(rankingListMessageDTO));

        rabbitTemplate.convertAndSend(RabbitContant.EXCHANGE_NAME, RabbitContant.ROUTING_KEY,JsonUtils.objectToJson(mqMessageDTO));

    }

    @Override
    public Integer getMessageType() {
        return MqMessageTypeEnum.COMMENT.getCode();
    }
}
